package mars.game.core;

import java.io.IOException;

import android.media.MediaPlayer;
import com.ksjourney.KSJourney;
import com.ksjourney.R;

public class MarsMusicPlayer
{
	
	public MediaPlayer playerMusic;
	public KSJourney KSJMusic = null;
	
	public MarsMusicPlayer(KSJourney KSJMusic)
	{
		this.KSJMusic = KSJMusic;
	}
	
	// 播放音乐
	
	public void PlayMusic(int ID)
	{

		FreeMusic();
		switch (ID)
		{
			case 1:
				// 装载音乐
				playerMusic = MediaPlayer.create(KSJMusic, 0x7f040001);// R.raw.music1不能用
				// 设置循环
				playerMusic.setLooping(true);
				try
				{
					playerMusic.prepare();
				}
				catch (IllegalStateException e)
				{
					e.printStackTrace();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
				// 开始
				playerMusic.start();
				break;
			case 2:
				// 装载音乐
				playerMusic = MediaPlayer.create(KSJMusic, R.raw.music2);// R.raw.music1不能用
				// 设置循环
				playerMusic.setLooping(true);
				try
				{
					playerMusic.prepare();
				}
				catch (IllegalStateException e)
				{
					e.printStackTrace();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
				// 开始
				playerMusic.start();
				break;
			case 3:
				playerMusic = MediaPlayer.create(KSJMusic, R.raw.music2);// R.raw.music1不能用
				// 设置循环
				playerMusic.setLooping(true);
				try
				{
					playerMusic.prepare();
				}
				catch (IllegalStateException e)
				{
					e.printStackTrace();
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
				// 开始
				playerMusic.start();
				break;
		}
		
	}
	
	// 退出释放资源
	private void FreeMusic()
	{

		if (playerMusic != null)
		{
			playerMusic.stop();
			playerMusic.release();
		}
	}
	
	// 停止播放
	public void StopMusic()
	{

		if (playerMusic != null)
		{
			playerMusic.stop();
		}
	}
}
